<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use app\common\model\User;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use think\Db;
use think\Log;

/**
 * 充值
 *
 * @icon fa fa-circle-o
 */
class Userscorelog extends Backend
{

    /**
     * Recharge模型对象
     * @var \app\admin\model\Userscorelog
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Userscorelog;
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model
                ->with(['User','Merchant'])
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }
    /**
     * 导出
     */
    public function export(){
//当前是否为关联查询
        $this->relationSearch = true;
        $filter = $this->request->param("filter");

        list($where, $sort, $order, $offset, $limit) = $this->buildparams();
        $filter = json_decode($filter,true);

        $map = [];
        if(isset($filter['information_id']))
        {
            $map['order.information_id'] = $filter['information_id'];
        }

        if(isset($filter['create_time']))
        {
            $start = substr($filter['create_time'],0,19);
            $end = substr($filter['create_time'],22);
            $map['order.create_time'] = ['between',[$start,$end]];
        }

        $map['status'] = 0;

        // 设置数据
        $lists = $this->model->alias('recharge')
            ->with(['User','Merchant'])
            ->field('merchant.name as merchant_name,user.nickname as user_nickname')
            ->where($where)
            ->order($sort, $order)
            ->select();

        $list = [];
        $str = ['未审核','审核通过','审核驳回'];
        $type = ['自由积分','固定积分'];
        foreach($lists as $key=>$item) {
            $list[$key]['merchant_name'] = $item['merchant_name']??'';
            $list[$key]['user_nickname'] = $item['user_nickname']??'';
            $list[$key]['score'] = $item['score']??'';
            $list[$key]['is_fixed'] = $type[$item['is_fixed']];
            $list[$key]['createtime'] = date('Y-m-d H:i:s',$item['createtime']);
            $list[$key]['memo'] =$item['memo'];
        }


        $spreadsheet = new Spreadsheet();


        $sheet = $spreadsheet->getActiveSheet();
        $sheet->getStyle('A:S')->getAlignment()
            ->setVertical(\PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER) //设置垂直居中
            ->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER) //设置水平居中
            ->setWrapText(true); //设置自动换行

        //设置表头
        $sheet->setCellValue('A1', '商家');
        $sheet->setCellValue('B1', '用户');
        $sheet->setCellValue('C1', '变动积分');
        $sheet->setCellValue('D1', '积分类型');
        $sheet->setCellValue('E1', '备注');
        $sheet->setCellValue('F1', '时间');




        $sheet->getColumnDimension('A')->setWidth(20);
        $sheet->getColumnDimension('B')->setWidth(20);
        $sheet->getColumnDimension('C')->setWidth(30);
        $sheet->getColumnDimension('D')->setWidth(20);
        $sheet->getColumnDimension('E')->setWidth(20);
        $sheet->getColumnDimension('F')->setWidth(20);



        $sheet->getDefaultRowDimension()->setRowHeight(30);
        $k = ['I','J','K','L','M','N','O','P','Q','R'];
        //输出表格
        foreach($list as $keys => $value) {

            $keys = $keys + 2;
            $sheet->setCellValue('A'.$keys,$value['merchant_name']??'');
            $sheet->setCellValue('B'.$keys,$value['user_nickname']??'');
            $sheet->setCellValue('C'.$keys,$value['score']??'');
            $sheet->setCellValue('D'.$keys,$value['is_fixed']??'');
            $sheet->setCellValue('E'.$keys,$value['memo']??'');
            $sheet->setCellValue('F'.$keys,$value['createtime']??'');

            //var_dump($value['images']);

        }
        $filename = '导出'.date('ymdhis',time()).'.xlsx';
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename="'.$filename.'"');
        header('Cache-Control: max-age=0');
        $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
        $writer->save('php://output');

        exit;
    }
}
